package org.dut.aoptest.Proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //静态代理，为每一个需要实现功能增强的方法手动增强
//        HouseProxy houseProxy =new HouseProxy(new RealHouseSubject());
//        houseProxy.saleHouse();
//        houseProxy.rentHouse();

        //动态代理 ——————JDK
        HouseSubject target = new RealHouseSubject();
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
//                target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocationHandler(target)
//        );
//        proxy.rentHouse();

        //动态代理 —————— CGLIB
        System.out.println("----------------------分割线--------------------");

//        HouseSubject target2 = new RealHouseSubject();
//        Java不允许CGlib访问java.base某些内部接口
        //CGLIB可以代理接口还有类
        HouseSubject proxy2 = (HouseSubject) Enhancer.create
                (target.getClass(),new CGLibInterceptor(target));
        proxy2.rentHouse();
        RealHouseSubject proxy3 = (RealHouseSubject) Enhancer.create
                (target.getClass(),new CGLibInterceptor(target));
        proxy2.rentHouse();
    }
}
